﻿/*
this class handles the way the characters scales based on the vanishing points
defined on the screen.
*/

class DepthAndScaleEngine.VanishingPoint{
	var oMovieClip:MovieClip;
	var arrDepthObjects:Array;
	var CFE:Number;	
	public function VanishingPoint(poMovieClip:MovieClip, pCFE:Number){
		if(pCFE==null){
			this.CFE = 25;	
		}else{
			this.CFE = pCFE;
		}
		this.oMovieClip = poMovieClip;
		arrDepthObjects = new Array();
		var oSelfReference:VanishingPoint = this;
		//---[ENTER FRAME DEFINITION]---
		this.oMovieClip.onEnterFrame = function(){
			oSelfReference.handleScale();
		}
	}
	function addObjectToScale(poMC:MovieClip){
		this.arrDepthObjects.push(poMC);
	}
	function handleScale() {
		var i:Number;
		//var this.arrDepthObjects:Array;
		var mc:MovieClip;
		var actorPoint:Object;
		var scaleCoeficient:Number;
		for (i = 0; i < this.arrDepthObjects.length; i++) {
			actorPoint = {
				x:this.arrDepthObjects[i]._x,
				y:this.arrDepthObjects[i]._y
			};
			_global.SCUMMGame.currentScene.mc.localToGlobal(actorPoint);
			if (this.oMovieClip.hitTest(actorPoint.x, actorPoint.y, true)) {
				scaleCoeficient = this.calculateScale(this.arrDepthObjects[i], this.CFE);
				this.arrDepthObjects[i]._yscale = scaleCoeficient;
				if(this.arrDepthObjects[i]._xscale < 0){
					this.arrDepthObjects[i]._xscale = scaleCoeficient*(-1);
				} else {
					this.arrDepthObjects[i]._xscale = scaleCoeficient;
				} 
				
				  
			}
		}
	}
	function calculateScale(pTargetObject:MovieClip, pnCFE:Number):Number {
		var d0:Number = distancia(this.oMovieClip, pTargetObject);
		var d:Number = this.oMovieClip._width/2;
		return (d0/d)*(100-pnCFE)+pnCFE;
	}
	function distancia(p0:MovieClip, p1:MovieClip) {
		return Math.sqrt((p0._x - p1._x) * (p0._x - p1._x) + (p0._y - p1._y) * (p0._y - p1._y));
	}
}